﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LandandSurvey
{
    public partial class Search1186Plans : System.Web.UI.Page
    {
        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !IsPostBack )
            {
                ddlArea.Items.Add( new ListItem( "Acre" ) );
                ddlArea.Items.Add( new ListItem( "HA" ) );
                ddlArea.Items.Add( new ListItem( "SF" ) );
                ddlArea.Items.Add( new ListItem( "SM" ) );

                ddlWardSheet.Items.Add( new ListItem( "Old" ) );
                ddlWardSheet.Items.Add( new ListItem( "New" ) );
            }
        }

        protected void btnSearch_Click( object sender, EventArgs e )
        {
            gvResults.DataSource = Search();
            gvResults.DataBind();
        }

        private List<Plan1186> Search()
        {
            landsdbEntities container = new landsdbEntities();

            var results = from i in container.Plan1186
                          select i;

            if ( txtBookNo.Text != string.Empty )
            {
                string criteria = txtBookNo.Text.Trim();
                results = from i in results
                          where i.BookNo.Contains( criteria )
                          select i;
            }

            if ( txtFolio.Text != string.Empty )
            {
                string criteria = txtFolio.Text.Trim();
                results = from i in results
                          where i.Folio.Contains( criteria )
                          select i;
            }

            if ( txtLocality.Text != string.Empty )
            {
                string criteria = txtLocality.Text.Trim();
                results = from i in results
                          where i.Locality.Contains( criteria )
                          select i;
            }

            if ( txtParcelOwner.Text != string.Empty )
            {
                string criteria = txtParcelOwner.Text.Trim();
                results = from i in results
                          where i.ParcelOwner.Contains( criteria )
                          select i;
            }

            if ( txtWardSheetNewSection.Text != string.Empty )
            {
                string criteria = txtWardSheetNewSection.Text.Trim();
                results = from i in results
                          where i.WardSheetNewSection.Contains( criteria )
                          select i;
            }
            //-------ddl---------
            if ( ddlArea.SelectedItem.Text == "Acre" )
            {
                string criteria = txtArea.Text.Trim();
                results = from i in results
                          where i.AreaAcre.Contains( criteria )
                          select i;
            }

            if ( ddlArea.SelectedItem.Text == "HA" )
            {
                string criteria = txtArea.Text.Trim();
                results = from i in results
                          where i.AreaHA.Contains( criteria )
                          select i;
            }

            if ( ddlArea.SelectedItem.Text == "SF" )
            {
                string criteria = txtArea.Text.Trim();
                results = from i in results
                          where i.AreaSF.Contains( criteria )
                          select i;
            }

            if ( ddlArea.SelectedItem.Text == "SM" )
            {
                string criteria = txtArea.Text.Trim();
                results = from i in results
                          where i.AreaSM.Contains( criteria )
                          select i;
            }

            if ( ddlWardSheet.SelectedItem.Text == "Old" )
            {
                string criteria = txtWardSheet.Text.Trim();
                results = from i in results
                          where i.WardSheetOld.Contains( criteria )
                          select i;
            }

            if ( ddlWardSheet.SelectedItem.Text == "New" )
            {
                string criteria = txtWardSheet.Text.Trim();
                results = from i in results
                          where i.WardSheetNew.Contains( criteria )
                          select i;
            }

            return results.ToList<Plan1186>();
        }
    }
}